package com.person.leetcode.editor.cn;

/**
 * 67.二进制求和
 * 2022-05-10 16:49:30
 **/
public class Sequence_0067_AddBinary {
    public static void main(String[] args) {
        Solution solution = new Sequence_0067_AddBinary().new Solution();
        int i = Integer.parseInt("10101011", 2);
        int j = Integer.parseInt("10111001", 2);
        System.out.println(i);
        System.out.println(j);
        System.out.println(i + j);
        System.out.println(Integer.toBinaryString(i + j));
        //1 1 1
        //1 1 1
      //1 1 1 0
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String addBinary(String a, String b) {
            int max = Math.max(a.length(), b.length());
            int carry = 0;
            StringBuilder res = new StringBuilder();
            StringBuilder aB = new StringBuilder(a).reverse();
            StringBuilder bB = new StringBuilder(b).reverse();

            for (int i = 0; i < max || carry != 0; i++) {
                int aNum = 0, bNum = 0;
                if (i < aB.length()) {
                     aNum = aB.charAt(i) - '0';
                }
                if (i < bB.length()) {
                     bNum = bB.charAt(i) - '0';
                }
                //当前下的数值
                int t = aNum + bNum + carry;// 0 1 10 11
                res.append(t%2);
                carry = t/2;
            }
            return res.reverse().toString();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
